package cn.lcsjsxy.yxc.exercise.java050529;

import java.util.*;

/*实现一个学生成绩分析系统，要求：

    存储学生姓名和对应的三门课程成绩

    计算每个学生的平均分

    找出所有科目平均分最高的学生

    找出有科目不及格的学生（低于 60 分）

    按学生排名排序输出所有学生信息*/
public class StudentGradeAnalyzer {

    //将学生分数变成list集合返回
    public static List<Double> StudentSelf(double mathe, double chineseLanguage, double englishLanguage) {
        List<Double> studentScore = new ArrayList<>();
        studentScore.add(mathe);
        studentScore.add(chineseLanguage);
        studentScore.add(englishLanguage);
        return studentScore;
    }

    public static Map<String, List<Double>> studentGrade(String name, List<Double> studentScore) {
        Map<String, List<Double>> map = new HashMap<>();
        map.put(name, studentScore);
        return map;
    }

    //存储学生姓名和对应的三门课程成绩
    public static Map<String, List<Double>> addStudent(Map<String, List<Double>> map) {
        Scanner scanner = new Scanner(System.in);
        for (; ; ) {
            String name = null;
            double mathe = 0;
            double chineseLanguage = 0;
            double englishLanguage = 0;

            System.out.print("请输入姓名：");
            if ((name = scanner.nextLine()) == null) {
                System.out.println("请不要输入负数！");
                continue;
            }
            System.out.print("请输入数学成绩：");
            if ((mathe = scanner.nextDouble()) < 0) {
                System.out.println("请不要输入负数！");
                continue;
            }
            System.out.print("请输入语文成绩：");
            if ((chineseLanguage = scanner.nextDouble()) < 0) {
                System.out.println("请不要输入负数！");
                continue;
            }
            System.out.print("请输入英语成绩：");
            if ((englishLanguage = scanner.nextDouble()) < 0) {
                System.out.println("请不要输入负数！");
                continue;
            }
            scanner.nextLine();
            map.putAll(studentGrade(name, StudentSelf(mathe, chineseLanguage, englishLanguage)));
            while (true) {
                System.out.println("是否继续添加：（Y/N）");
                String answer = scanner.nextLine();
                if ("Y".equals(answer) || "y".equals(answer)) {
                    System.out.println("继续添加！");
                    break;
                }
                if ("N".equals(answer) || "n".equals(answer)) {
                    System.out.println("添加结束！");
                    return map;
                } else {
                    System.out.println("请输入正确的字符！（Y/N）");
                }
            }
        }
    }

    //计算每个学生的平均分
    public static Map<String, Double> averageScore(Map<String, List<Double>> map) {
        if (map == null || map.size() == 0) {
            System.out.println("无数据！");
            return null;
        }
        String avg = null;
        Map<String, Double> averageScore = new HashMap<>();
        for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
            double sum = 0;
            String key = entry.getKey();
            List value = entry.getValue();
            for (Object d : value) {
                sum += Double.parseDouble(d.toString());
            }
            avg = String.format("%.2f", sum / value.size());

            sum = new Double(avg).doubleValue();
            averageScore.put(key, sum);
        }
        return averageScore;
    }

    //找出所有科目平均分最高的学生
    public static Map<String, Double> averageMaxStudent(Map<String, List<Double>> map) {
        if (map == null || map.size() == 0) {
            System.out.println("数据空！");
            return null;
        }
        Map<String, Double> averageScore = averageScore(map);
        double maxScore = 0;
        String maxKey = null;
        for (Map.Entry<String, Double> entry : averageScore.entrySet()) {
            if (entry.getValue() > maxScore) {
                maxScore = entry.getValue();
                maxKey = entry.getKey();
            }
        }
        Map<String, Double> averageMaxStudent = new HashMap<>();
        averageMaxStudent.put(maxKey, maxScore);
        return averageMaxStudent;
    }

    //找出有科目不及格的学生（低于 60 分）
    public static Map<String, String> failStudent(Map<String, List<Double>> map) {
        if (map == null || map.size() == 0) {
            System.out.println("数据空！");
            return null;
        }
        Map<String, String> failStudent = new HashMap<>();
        for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
            String key = entry.getKey();
            List value = entry.getValue();
            int cont = 0;
            int subject = 0;
            for (Object d : value) {
                subject++;
                double score = 0;
                if (d instanceof Double) {
                    score = ((Double) d).doubleValue();
                }
                if (score < 60.0) {
                    cont++;
                }
            }
            if (cont > 0) {
                switch (subject) {
                    case 1:
                        failStudent.put(key, "有" + cont + "门科目不及格\t不及格科目：数学");
                        break;
                    case 2:
                        failStudent.put(key, "有" + cont + "门科目不及格\t不及格科目：语文");
                        break;
                    case 3:
                        failStudent.put(key, "有" + cont + "门科目不及格\t不及格科目：英语");
                }
            }
        }
        if (failStudent.size() == 0 || failStudent == null) {
            System.out.println("没有不及格的学生！");
            return null;
        }
        return failStudent;
    }

    //按照排名打印顺序
    public static void printStudentScore(Map<String, List<Double>> map) {
        if (map == null || map.size() == 0) {
            System.out.println("数据空！");
            return;
        }
        String[] studentName = new String[map.size()];
        Double[] studentScore = new Double[map.size()];
        int i = 0;
        Map<String, Double> average = averageScore(map);
        for (Map.Entry<String, Double> entry : average.entrySet()) {
            String key = entry.getKey();
            double score = entry.getValue();
            studentScore[i] = score;
            studentName[i] = key;
            i++;
        }
        for (int index = 0; index < studentName.length - 1; index++) {
            for (int j = 0; j < studentName.length - index - 1; j++) {
                if (studentScore[j] < studentScore[j + 1]) {
                    double temp = studentScore[j];
                    studentScore[j] = studentScore[j + 1];
                    studentScore[j + 1] = temp;
                    String tempKey = studentName[j];
                    studentName[j] = studentName[j + 1];
                    studentName[j + 1] = tempKey;
                }
            }
        }

        for (int index = 0; index < studentName.length; index++) {
            System.out.println((index + 1) + "\t" + studentName[index] + "\t" + studentScore[index]);
        }
    }
}
